﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using ZHDJ.Core.Util;

namespace ZHDJ.Data.Models.Org
{
    /// <summary>
    /// 用户基础信息配置
    /// </summary>
    public class PartyOrgMemberSetting:EntityBase
    {
        #region
        public string LoginID { get; set; }

        /// <summary>
        /// 设置类型
        /// </summary>
        public string SettingType { get; set; }

        /// <summary>
        /// 设置值
        /// </summary>
        public string SettingValue { get; set; }

        #endregion

        #region 数据操作

        /// <summary>
        /// 获取用户配置值
        /// </summary>
        /// <param name="loginID">用户ID</param>
        /// <param name="settingType">配置类型</param>
        /// <returns></returns>
        public static string GetSettingValue(string loginID, string settingType)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "LoginID",
                    Value = loginID
                },
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "SettingType",
                    Value = settingType
                }
            };
            string sql = string.Format(@" select SettingValue from tblPartyOrgMemberSetting where LoginID=?LoginID and  SettingType=?SettingType ");
            object obj = MySqlHelper.ExecuteScalar(sql, parameters.ToArray());
            if (obj != null) return obj.ToString();
            else return "";
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="settingType"></param>
        /// <param name="settingValue"></param>
        /// <returns></returns>
        public static string SaveSettingValue(string loginID, string settingType,string settingValue)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "LoginID",
                    Value = loginID
                },
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "SettingType",
                    Value = settingType
                }
                ,
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "SettingValue",
                    Value = settingValue
                },
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "ID",
                    Value = StringHelper.NewID
                }
            };
            string sql = string.Format(@" delete from tblPartyOrgMemberSetting where LoginID=?LoginID and  SettingType=?SettingType;
                            insert into tblPartyOrgMemberSetting(ID,LoginID,SettingType,SettingValue) values(?ID,?LoginID,?SettingType,?SettingValue); ");
            object obj = MySqlHelper.ExecuteScalar(sql, parameters.ToArray());
            if (obj != null) return obj.ToString();
            else return "";
        }
        #endregion

    }

}
